<template>
	<view>
		<view class="bg"></view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				id: '', //活动ID
				pUid: '', //do支付完成后带回来的抖音用户uid
				// sign: '', //有 sign=1 代表是H5支付完成后重定向过来的 要直接跳转 previewPush 页面

				merchantId: '',

			}
		},
		onLoad(opinons) {
        // console.log("===", window.location.href);
        // // 抖音接受一个活动id
		  //  this.id=opinons.activityId
		  // if(opinons.activityListId){
			 //   this.pUid=  opinons.activityListId
			 //   uni.reLaunch({
			 //   	url: '/pages/index/merchantDetails?activityId='+this.id+'&activityListId='+this.pUid
			 //   })
		  // }else{
			 //   this.getJssdkConfig();
		  // }
		},
		onShow() {
			
			let getUrl = window.location.href.split("#")[0];
			// console.log(getUrl)
			// let getUrl='https://yuntt.weijuyunke.com/business/mobile/doPay_index_sign?activityId=a66a86325ccc47fdbddd8dec0b1a019d&activityListId=de81ced6e75f4e1a84a8e13777abc91b'.split("#")[0]
			let one = getUrl.split("?activityId=")[1];
			if (one.indexOf("&activityListId=") == -1) {
				this.id = one;
				this.getJssdkConfig();
			} else {
				this.id = one.split("&activityListId=")[0];

				this.pUid = one.split("&activityListId=")[1];
				uni.showModal({
					content: uni.getStorageSync('merchantId'),
					showCancel: false,
					confirmText: '确定',
					success() {
						window.close();
					}
				})
				uni.reLaunch({
					url: '/pages/index/merchantDetails?activityId='+this.id+'&activityListId='+this.pUid+'&merchantId='+ uni.getStorageSync('merchantId')
				})
			}
			// uni.reLaunch({
			// 	url: '/pages/index/pay?id=a66a86325ccc47fdbddd8dec0b1a019d&uid=cec257b2edc84ed4ad09c06dd4635363&merchantId=476&uaType=bytedancewebview'
			// })
		},
		methods: {

			async getJssdkConfig() {

				let ua = window.navigator.userAgent.toLowerCase();
				let uaType = '';

				if (ua.indexOf("bytedancewebview") != -1) {
					uaType = 'bytedancewebview';
				} else {
					uni.showModal({
						content: "请打开抖音APP,使用扫一扫功能扫码进入!",
						showCancel: false,
						confirmText: '确定',
						success() {
							window.close();
						}
					})
					return;
				}
				
				const reuslt = await this.$http.post({
					url: this.$api.douPayH5Md5,
					data: {
						url: window.location.href.split("#")[0],
						activityId: this.id
					}
				});
                this.merchantId = reuslt.data.merchantId;
				
				uni.setStorageSync('merchantId', this.merchantId)
                
				this.$dy.config({
					params: {
						client_key: reuslt.data.clientKey, // clientKey在你的网页应用申请通过后得到
						signature: reuslt.data.signature, // 服务端计算的签名，该签名被抖音开放平台验证通过后方可调用jsb方法
						timestamp: reuslt.data.timestamp, //时间戳
						nonce_str: reuslt.data.nonceStr,
						url: reuslt.data.url // 为应用申请的 JSB 安全域名下的链接，需要携带协议。e.g. https://jsb.security.domain/page.html
					},
				});
				this.$dy.ready(() => {
					// uni.showModal({
					// 	content: "签名验证成功==>"
					// })
				});
				this.$dy.error((res) => {
					uni.showModal({
						content: "签名验证失败回调",
						showCancel: false,
						confirmText: '确定',
						success() {

						}
					})
					return
				});
				setTimeout(() => {
					this.$dy.showOpenAuth({
						params: {
							client_key: reuslt.data.clientKey, // clientKey 在你的网页应用申请通过后得到
							state: "", // 自定义状态，详见下方说明
							scopes: {
								user_info: 0, // 0: 必选；1: 可选，默认不选中； 2: 可选，默认选中
								"data.external.item": 0
							},
							response_type: "code", // 默认填‘code’，详见下方说明
						},
						success: async ({
							ticket,
							grant_permissions
						}) => {
							
							const userResult = await this.$http.get({
								url: this.$api.accountToken,
								data: {
									code: ticket,
									activityId: this.id
								}
							});
                                
							if (userResult.code == 2000) {
								uni.showModal({
									content: "成功  ==>" + userResult.msg,
									showCancel: false,
									confirmText: '确定',
									success() {
										window.close();
									}
								})
								uni.hideLoading();
								uni.reLaunch({
									url: '/pages/index/pay?id=' + this.id +'&uid='+userResult.data.activityListId+'&merchantId='+ this.merchantId+'&uaType='+uaType
								})
							
							} else {
								uni.showModal({
									content: "用户保存失败  ==>" + userResult.msg,
									showCancel: false,
									confirmText: '确定',
									success() {
										window.close();
									}
								})
							}
						},
						error: (res) => {
							uni.showModal({
								content: "授权验证失败,请检查D音授权配置",
								showCancel: false,
								confirmText: '确定',
								success() {
									window.close();
								}
							})
						},
					});

				}, 2000);

			},

		}
	}
</script>

<style lang="scss">
	.bg {
		background: url("~static/110.jpg");
		width: 750rpx;
		height: 1444rpx;
		background-repeat: no-repeat;
		background-size: contain;
	}

	page {
		background-color: #00074d;
	}
</style>